package com.link2room.soundwave.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.dto.param.inside.PmsRmtypeInsideParamDto;
import com.link2room.soundwave.dto.result.base.PmsGroupRmtypeDto;
import com.link2room.soundwave.entity.L2RIpms2Group;
import com.link2room.soundwave.entity.L2RYhHotel;
import com.link2room.soundwave.entity.L2RYhRmtype;
import com.link2room.soundwave.service.IYhPmsBaseService;
import org.springframework.stereotype.Component;
import yuheng.pms.result.YhRmtypeDto;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author sqj88
 */
@Component("yhPmsRmtypeHandlerFacadeService")
public class YhPmsRmtypeHandlerFacadeServiceImpl extends AbstractPmsRmtypeHandlerFacadeServiceImpl {
    @Resource
    private ICommonService commonService;
    @Resource
    private IYhPmsBaseService yhPmsBaseService;

    @Override
    public void saveOrUpdateRmtype(PmsRmtypeInsideParamDto pmsRmtypeInsideParamDto) throws Exception {
        L2RYhHotel l2RYhHotel = new L2RYhHotel();
        l2RYhHotel.setLhotelCode(pmsRmtypeInsideParamDto.getLhotelCode() );
        l2RYhHotel.setLhotelGroupCode(pmsRmtypeInsideParamDto.getLhotelGroupCode());
        l2RYhHotel= commonService.findOneEQ(L2RYhHotel.class,l2RYhHotel );

        L2RYhRmtype find = new L2RYhRmtype();
        find.setLhotelGroupCode( pmsRmtypeInsideParamDto.getLhotelGroupCode());
        find.setLhotelCode( pmsRmtypeInsideParamDto.getLhotelCode());
        find.setL2RRmtype(pmsRmtypeInsideParamDto.getL2rRmtype());
        L2RYhRmtype l2RYhRmtype = commonService.findOneEQ(L2RYhRmtype.class, find );

        if( null== l2RYhRmtype){
            Optional.ofNullable(l2RYhHotel).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_MAPPINGHOTEL_NOT_EXIT));
            l2RYhRmtype=new L2RYhRmtype();
            l2RYhRmtype.setLhotelGroupCode( pmsRmtypeInsideParamDto.getLhotelGroupCode());
            l2RYhRmtype.setLhotelCode( pmsRmtypeInsideParamDto.getLhotelCode());
            l2RYhRmtype.setL2RRmtype(pmsRmtypeInsideParamDto.getL2rRmtype());
            l2RYhRmtype.setL2RRmtypeName( pmsRmtypeInsideParamDto.getL2rRmtypeName());
            l2RYhRmtype.setPmsRmtype(pmsRmtypeInsideParamDto.getPmsRmtype());
            l2RYhRmtype.setHotelCode( l2RYhHotel.getHotelCode().toString());
            l2RYhRmtype.setHotelGroupCode( l2RYhHotel.getGroupCode());
        }else {
            l2RYhRmtype.setPmsRmtype(pmsRmtypeInsideParamDto.getPmsRmtype());
            l2RYhRmtype.setL2RRmtypeName(pmsRmtypeInsideParamDto.getL2rRmtypeName());
        }
        commonService.saveOrUpdate( l2RYhRmtype);
    }

    @Override
    QueryResult<PmsGroupRmtypeDto> listPmsGroupRmtype(BaseParamDto baseParamDto) throws Exception {
        String lhotelGroupCode=baseParamDto.getLhotelGroupCode();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        QueryResult<PmsGroupRmtypeDto> pmsGroupRmtypeDtoQueryResult=new QueryResult<>();
        List<PmsGroupRmtypeDto> pmsGroupRmtypeDtoList=null;
        if(l2RIpms2Group!=null) {
            String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
            QueryResult<YhRmtypeDto> yhRmtypeDtoQueryResult = yhPmsBaseService.getYhRmtypeList(lhotelGroupCode, pmsHotelGroupCode, null,
                    baseParamDto.getCurrentPage(),
                    baseParamDto.getPageSize());
            PmsGroupRmtypeDto pmsGroupRmtypeDto=null;
            if(yhRmtypeDtoQueryResult.getResultlist()!=null){
                pmsGroupRmtypeDtoList=new ArrayList<>();
                Set<String> rmtypeSet=new HashSet<>();
                for(YhRmtypeDto yhRmtype:yhRmtypeDtoQueryResult.getResultlist()){
                    if(!rmtypeSet.contains(yhRmtype.getRoomType())){
                        pmsGroupRmtypeDto=new PmsGroupRmtypeDto();
                        pmsGroupRmtypeDto.setDescript(yhRmtype.getDescription());
                        pmsGroupRmtypeDto.setGroupRmtype(yhRmtype.getRoomType());
                        pmsGroupRmtypeDto.setName(yhRmtype.getDescription());
                        pmsGroupRmtypeDto.setLhotelGroupCode(lhotelGroupCode);
                        pmsGroupRmtypeDto.setLhotelCode(DefaultValue.LHOTEL_CODE);
                        rmtypeSet.add(yhRmtype.getRoomType());
                        pmsGroupRmtypeDtoList.add(pmsGroupRmtypeDto);
                    }
                }
                pmsGroupRmtypeDtoQueryResult.setResultlist(pmsGroupRmtypeDtoList);
                pmsGroupRmtypeDtoQueryResult.setCurrentPage(baseParamDto.getCurrentPage());
                pmsGroupRmtypeDtoQueryResult.setTotalrecord(yhRmtypeDtoQueryResult.getTotalrecord());
            }
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
        return pmsGroupRmtypeDtoQueryResult;
    }
}
